package com.hcinfos.gsoo.ui;

import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hcinfos.gsoo.R;
import com.hcinfos.gsoo.config.Common;
import com.hcinfos.gsoo.config.Urls;
import com.hcinfos.gsoo.https.HttpClientUtil;
import com.hcinfos.gsoo.util.LogUtil;

public class AgreeActivity extends Activity implements OnClickListener {

	private TextView agreeTv;
	private RelativeLayout progress_layout;
	private LinearLayout loading_dialog_content_layout;
	private RelativeLayout loading_staus_rl;
	private TextView status_tv;

	private TextView reg_tv;
	private ImageButton left_ib;
	private TextView center_tv;

	private final int GET_AGREE_SUCCESS = 0x1120;
	private final int GET_AGREE_FAIL = GET_AGREE_SUCCESS + 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		int width = this.getWindowManager().getDefaultDisplay().getWidth();
		Common.PROPORTION = width / 320f;
		Common.WIDTH = width;
		setContentView(R.layout.activity_agree);
		left_ib = (ImageButton) findViewById(R.id.left_ib);
		reg_tv = (TextView) findViewById(R.id.reg_tv);
		left_ib.setOnClickListener(this);
		reg_tv.setOnClickListener(this);
		reg_tv.setVisibility(View.GONE);
		center_tv = (TextView) findViewById(R.id.center_tv);
		center_tv.setText("即搜协议");
		agreeTv = (TextView) findViewById(R.id.agreeTv);
		progress_layout = (RelativeLayout) findViewById(R.id.progress_layout);
		progress_layout.setOnClickListener(this);
		progress_layout.setVisibility(View.VISIBLE);
		loading_dialog_content_layout = (LinearLayout) findViewById(R.id.loading_dialog_content_layout);
		loading_staus_rl = (RelativeLayout) findViewById(R.id.loading_staus_rl);
		loading_staus_rl.setVisibility(View.GONE);// 内容提示
		loading_dialog_content_layout.setVisibility(View.VISIBLE);// 加载条
		status_tv = (TextView) findViewById(R.id.status_tv);
	}

	@Override
	protected void onResume() {

		obtainAgree();
		super.onResume();
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.progress_layout:
			loading_staus_rl.setVisibility(View.VISIBLE);
			loading_dialog_content_layout.setVisibility(View.GONE);
			obtainAgree();
			break;
		case R.id.left_ib:
			finish();
			break;
		}
	}

	private void obtainAgree() {
		new Thread(new Runnable() {

			@Override
			public void run() {
				try {
					String result = HttpClientUtil.getFromWebByHttpClient(
							AgreeActivity.this, Urls.WEB_AGREE);
					LogUtil.d("wxf", "result" + result);
					if (result == null) {
						Message msg = handler.obtainMessage();
						msg.what = GET_AGREE_FAIL;
						handler.sendMessage(msg);
					} else {
						JSONObject obj = new JSONObject(result);
						String agreeStr = obj.getJSONObject("data").getString(
								"val");
						Message msg = handler.obtainMessage();
						msg.what = GET_AGREE_SUCCESS;
						msg.obj = agreeStr;
						handler.sendMessage(msg);
					}
				} catch (Exception e) {
					Message msg = handler.obtainMessage();
					msg.what = GET_AGREE_FAIL;
					handler.sendMessage(msg);
				}
			}
		}).start();
	}

	Handler handler = new Handler() {

		public void handleMessage(android.os.Message msg) {

			switch (msg.what) {
			case GET_AGREE_SUCCESS:
				progress_layout.setVisibility(View.GONE);
				if (msg.obj != null) {
					agreeTv.setText(Html.fromHtml(msg.obj.toString()));
				} else {
					agreeTv.setText("暂无协议");
				}
				break;
			case GET_AGREE_FAIL:
				status_tv.setText("协议加载失败");
				loading_staus_rl.setVisibility(View.VISIBLE);
				loading_dialog_content_layout.setVisibility(View.GONE);
				break;
			}
		};
	};
}
